home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / intrvews / xgrab.lha / xgrab / ui / insert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-07  |  2.6 KB  |  143 lines

  1. /**
  2.    GRAB Graph Layout and Browser System
  3.  
  4.    Copyright (c) 1986, 1988 Regents of the University of California
  5.    Copyright (c) 1989, Tera Computer Company
  6.  **/
  7.  
  8.   /** 
  9.      Contains routines for inserting nodes.
  10.    **/
  11.  
  12. #include "attribute.h"
  13. #include "digraph.h"
  14. #include "screen.h"
  15. #include "scrdep.h"
  16. #include "globals.h"
  17. #include "interf.h"
  18.  
  19. extern BOOL showbc;
  20. extern DIGRAPH *digraph, *InitDigraph();
  21. short hash();
  22. NODE *CreateNode();
  23. char *UniqueName();
  24.  
  25. int DoInsert();
  26.  
  27. void DoSInsertNode()
  28. {
  29.     if (digraph == NULL) 
  30.       /* first node on an empty screen */
  31.     {
  32.         digraph = InitDigraph();
  33.     }
  34.  
  35.     ChangeText(DoInsert);
  36. }
  37.  
  38. static char newtext[MAXSTR];
  39.  
  40. DoInsert()
  41. {
  42.     NODE *newnode;
  43.     char newname[MAXSTR];
  44.     int x, y, newx, newy;
  45.  
  46.     IGetCurPos(&x, &y);
  47.  
  48.     newx = SCRX_TO_ABSX(&screen, x);
  49.     newy = SCRY_TO_ABSY(&screen, y);
  50.  
  51.     if (digraph == NULL)
  52.     {
  53.         digraph = InitDigraph();
  54.     }
  55.  
  56.     strcpy(newname, newtext);
  57.  
  58.     if (!Unique(newtext)) 
  59.     {
  60.         strcpy(newname, UniqueName());
  61.     }
  62.  
  63.     newnode = CreateNode(digraph, newname, newtext, newx, newy);
  64.  
  65.       /* display this node */
  66.     draw_node(digraph, newnode, &screen, TRUE);
  67.  
  68.     IChangeStatusLine("Node Inserted", FALSE);
  69.     graphChanged = TRUE;
  70.     ckpt_done = FALSE;
  71. }
  72.  
  73. int EndChangeText();
  74. static int (*docommand)();
  75.  
  76. ChangeText(command)
  77. int (*command)();
  78. {
  79.     docommand = command;
  80.     IChangeStatusLine("Type node name", FALSE);
  81.  
  82.     TakeTextInput(EndChangeText);
  83. }
  84.  
  85. EndChangeText()
  86. {
  87.     OutofText(newtext);
  88.  
  89.     if (newtext[0] != '\0') 
  90.     {
  91.         if (docommand != NULL) 
  92.     {
  93.             (*docommand)();
  94.     }
  95.     }
  96.     else 
  97.     {
  98.         IChangeStatusLine("No node created", FALSE);
  99.     }
  100. }
  101.  
  102.   /**
  103.      Unique - returns TRUE if text is unique, else returns FALSE
  104.    **/
  105.  
  106. Unique(name)
  107. char *name;
  108. {
  109.     short hashcode;        /* hash code for name */
  110.     VERTEX *curvertex;     /* used to chase down the vertex chain */
  111.  
  112.     hashcode = hash(name);
  113.  
  114.       /* chase down the hash list, if any */
  115.     for (curvertex = digraph->hashtbl[hashcode];
  116.          (curvertex != NULL) && (strcmp(curvertex->name, name) != 0);
  117.      curvertex = curvertex->next)
  118.     {
  119.     }
  120.     
  121.     /**
  122.        Only way out of for loop is if curvertex == NULL (name not found)
  123.        or found name (and curvertex != NULL)
  124.        therefore, just need to return NOT of curvertex for true/false
  125.        indicator
  126.      **/
  127.  
  128.     return ((int) (! (curvertex)));
  129. }
  130.  
  131. char *UniqueName()
  132. {
  133.     static char newname[MAXSTR];
  134.     static int uniquenamecounter = 0;
  135.  
  136.     do 
  137.     {
  138.         sprintf(newname, "node%d",  ++uniquenamecounter);
  139.     } while (! Unique(newname));
  140.  
  141.     return (newname);
  142. }
  143.